<?php
 /* CAT:Pie charts */
@header('Content-Type: text/html; charset=utf-8');
 /* pChart library inclusions */
 include("../class/pData.class.php");
 include("../class/pDraw.class.php");
 include("../class/pPie.class.php");
 include("../class/pImage.class.php");

  $result = GetMaxs();
  foreach($result as $every){
	$sum = $sum + $every[1];
  }
 /* Create and populate the pData object */
 $MyData = new pData();   
 $MyData->addPoints(array($result[0][1],$result[1][1],$result[2][1],$result[3][1],$result[4][1],$result[5][1],$result[6][1],$result[7][1]),"ScoreA");  
 $MyData->setSerieDescription("ScoreA","Application A");

 /* Define the absissa serie */
 $MyData->addPoints(array($result[0][0],$result[1][0],$result[2][0],$result[3][0],$result[4][0],$result[5][0],$result[6][0],$result[7][0]),"Labels");
 $MyData->setAbscissa("Labels");

 /* Create the pChart object */
 $myPicture = new pImage(300,260,$MyData);

 /* Draw a solid background */
 $Settings = array("R"=>170, "G"=>183, "B"=>87, "Dash"=>1, "DashR"=>190, "DashG"=>203, "DashB"=>107);
 $myPicture->drawFilledRectangle(0,0,300,300,$Settings);

 /* Overlay with a gradient */
 $Settings = array("StartR"=>219, "StartG"=>231, "StartB"=>139, "EndR"=>1, "EndG"=>138, "EndB"=>68, "Alpha"=>50);
 $myPicture->drawGradientArea(0,0,300,260,DIRECTION_VERTICAL,$Settings);
 $myPicture->drawGradientArea(0,0,300,20,DIRECTION_VERTICAL,array("StartR"=>0,"StartG"=>0,"StartB"=>0,"EndR"=>50,"EndG"=>50,"EndB"=>50,"Alpha"=>100));

 /* Add a border to the picture */
 $myPicture->drawRectangle(0,0,299,259,array("R"=>0,"G"=>0,"B"=>0));

 /* Write the picture title */ 
 $myPicture->setFontProperties(array("FontName"=>"../fonts/MSYH.ttf","FontSize"=>6));
 $myPicture->drawText(10,13,"关键字搜索统计",array("R"=>255,"G"=>255,"B"=>255));

 /* Set the default font properties */ 
 $myPicture->setFontProperties(array("FontName"=>"../fonts/MSYH.ttf","FontSize"=>10,"R"=>80,"G"=>80,"B"=>80));

 /* Enable shadow computing */ 
 $myPicture->setShadow(TRUE,array("X"=>2,"Y"=>2,"R"=>0,"G"=>0,"B"=>0,"Alpha"=>50));

 /* Create the pPie object */ 
 $PieChart = new pPie($myPicture,$MyData);

 /* Draw an AA pie chart */ 
 $PieChart->draw2DRing(160,140,array("WriteValues"=>TRUE,"ValueR"=>255,"ValueG"=>255,"ValueB"=>255,"Border"=>TRUE));

 /* Write the legend box */ 
 $myPicture->setShadow(FALSE);
 $PieChart->drawPieLegend(15,40,array("Alpha"=>20));

 /* Render the picture (choose the best way) */
 $myPicture->autoOutput("pictures/example.draw2DRingValue.png");
 
  function GetMaxs(){
	$con = mysql_connect("127.0.0.1","root","redhat");
	if (!$con)
	{
		die('Could not connect: ' . mysql_error());
	}

	mysql_select_db("kfcmenu", $con);
	mysql_query("set names utf8");

	$query_str = "select keyword,search_times from keyinfo order by search_times desc;";
	$db_result = mysql_query($query_str);

	$result = array();
	while($row = mysql_fetch_array($db_result))
	{
		/*$times = $row['search_times'];
		$k = $row['keyword'];
		$result[$k] = $times;*/
		$temp = array($row['keyword'], $row['search_times']);
		array_push($result, $temp);
	}
	
	mysql_close($con);
	return $result;
}
?>
